package com.wfm.context;

import com.wfm.increment.DailyTimestampParamIncrementer;
import java.util.Map;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.JobSynchronizationManager;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableBatchProcessing
public class ContextJob {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    JobBuilderFactory jobBuilderFactory;

    @Autowired
    StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet tasklet(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step1...");
                // 步骤上下文
                ExecutionContext executionContext = chunkContext.getStepContext().getStepExecution()
                        .getExecutionContext();
                executionContext.put("step1","step1");

                // 作业上下文
                ExecutionContext executionContext1 = chunkContext.getStepContext().getStepExecution().getJobExecution()
                        .getExecutionContext();
                executionContext1.put("job","job");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Step step1(){
        return stepBuilderFactory.get("step1")
                .tasklet(tasklet())
                .build();
    }

    @Bean
    public Tasklet tasklet2(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step2...");
                ExecutionContext executionContext = chunkContext.getStepContext().getStepExecution()
                        .getExecutionContext();
                String step1 = (String)executionContext.get("step1");
                System.out.println(step1);

                ExecutionContext executionContext1 = chunkContext.getStepContext().getStepExecution().getJobExecution()
                        .getExecutionContext();
                String job = (String)executionContext1.get("job");
                System.out.println(job);
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Step step2(){
        return stepBuilderFactory.get("step2")
                .tasklet(tasklet2())
                .build();
    }

    @Bean
    public Job job(){
        return jobBuilderFactory.get("Context-job1")
                .incrementer(new DailyTimestampParamIncrementer())
                .start(step1())
                .next(step2())
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(ContextJob.class,args);

    }
}
